using Furion;
using Furion.Schedule;
using Furion.VirtualFileServer;
using JQData.Application.DataFetch;
using JQData.Application.Jobs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace JQData.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConsoleFormatter();
            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();

            services.AddControllersWithViews()
                        .AddInjectWithUnifyResult();

            // 添加Redis分布式缓存
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = App.Configuration.GetConnectionString("Redis");
                options.InstanceName = "JQData_";
            });

            services.AddSchedule(options =>
            {
                //串行执行
                options.AddJob<DataSync>("DataSync", false, Triggers.Period(1000 * 60));
                //options.AddJob<HotMoneyHandle>("HotMoneyHandle", false, Triggers.Period(15000));

                options.AddJob<RefreshCache>("RefreshCache", false,
                    Triggers.Period(1000 * 60 * 30)
                    , Triggers.DailyAt(18).SetRunOnStart(true));

            });
            services.AddScoped<HotMoneyDataFetch>();
            services.AddScoped<HotMoneyStockDataFetch>();
            services.AddScoped<DataCacheManage>();




        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
